#include <bits/stdc++.h>
//#define int long long
using namespace std;
const int N=2e5+5,M=2e6;
int T,n,arr[N],num[M];
long long dp[N],ans;

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);

    cin>>T;
    while(T--)
    {
        memset(num,0,sizeof(num));
        ans=0;

        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>arr[i];
            if(arr[i]==arr[i-1])
            {
                ans+=arr[i];
                dp[i]=dp[i-1];
            }
            else if(dp[i-1]!=dp[i-2]&&arr[i]==arr[i-2])
                dp[i]=dp[i-1]+arr[i];
            else if(dp[num[arr[i]]]!=dp[num[arr[i]]+1])
                dp[i]=max(dp[i-1],dp[num[arr[i]]+1]+arr[i]);
            else
            {
                if(num[arr[i]]!=0)
                    dp[i]=max(dp[i-1],dp[num[arr[i]]]+arr[i]);
                else
                    dp[i]=dp[i-1];
            }
            num[arr[i]]=i;
            //cout<<dp[i]<<" ";
        }
        //cout<<endl;
        cout<<dp[n]+ans<<endl;
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}
//
